#include <graphics.h>      
#include <conio.h>
#include<ctime>
#include<cstdlib>
#include<cmath>

int x = 20, y = 20;
int x2 = 1480, y2 = 580;
char direction = ' r ';
bool position[1505][605];
const int sleepTime = 0;

void left();
void right();
void up();
void down();
int getRandData(int a, int b);
bool Judge(int x, int y);
bool Judge2(int a, int b);
void displayGrass();
void displayBox();

int main()
{
	initgraph(1500, 600);
	displayGrass();
	displayBox();

	setcolor(YELLOW);
	setfillcolor(YELLOW);
	fillcircle(x, y, 10);


	char c = 0;
	while (1) {

		if (_kbhit()) {
			c = _getch();

			//移动
			if (c == 'a') {//left
				for (int i = 1;i<=5; x -= 2,i++) {
					if (x == 10 || position[x - 10][y])
						break;
					setcolor(YELLOW);
					setfillcolor(YELLOW);
					fillcircle(x, y, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x, y, 10);
				}
				direction = 'l';
				setcolor(YELLOW);
				setfillcolor(YELLOW);
				fillcircle(x, y, 10);
				continue;
			}
			if (c == 'd') {//right
				for (int i=1;i<=5;i++, x += 2) {
					if (x == 1490 || position[x + 10][y])
						break;
					setcolor(YELLOW);
					setfillcolor(YELLOW);
					fillcircle(x, y, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x, y, 10);
				}
				direction = 'r';
				setcolor(YELLOW);
				setfillcolor(YELLOW);
				fillcircle(x, y, 10);
				continue;
			}
			if (c == 'w') {//up
				for (int i=1;i<=5;i++, y -= 2) {
					if (y == 10 || position[x][y - 10])
						break;
					setcolor(YELLOW);
					setfillcolor(YELLOW);
					fillcircle(x, y, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x, y, 10);
				}
				direction = 'u';
				setcolor(YELLOW);
				setfillcolor(YELLOW);
				fillcircle(x, y, 10);
				continue;
			}
			if (c == 's') {//down
				for (int i=1;i<=5;i++, y += 2) {
					if (y == 590 || position[x][y + 10])
						break;
					setcolor(YELLOW);
					setfillcolor(YELLOW);
					fillcircle(x, y, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x, y, 10);
				}
				direction = 'd';
				setcolor(YELLOW);
				setfillcolor(YELLOW);
				fillcircle(x, y, 10);
				continue;
			}

			//攻击
			if (c == 'j') {
				int bx=x+2, by=y+2, br=2, btime=1;
				if (direction == 'l') {
					for (;; bx -= 2) {
						if (bx == 10 || position[bx - 10][by])
							break;
						setcolor(RED);
						setfillcolor(RED);
						fillcircle(bx, by, br);
						Sleep(btime);
						setcolor(BLACK);
						setfillcolor(BLACK);
						fillcircle(bx, by, br);
					}
					continue;
				}
				if (direction == 'r') {
					for (;; bx += 2) {
						if (bx == 1490 || position[bx + 10][by])
							break;
						setcolor(RED);
						setfillcolor(RED);
						fillcircle(bx, by, br);
						Sleep(btime);
						setcolor(BLACK);
						setfillcolor(BLACK);
						fillcircle(bx, by, br);
					}
					continue;
				}
				if (direction == 'u') {
					for (;; by -= 2) {
						if (by == 10 || position[bx][by - 10])
							break;
						setcolor(RED);
						setfillcolor(RED);
						fillcircle(bx, by, br);
						Sleep(btime);
						setcolor(BLACK);
						setfillcolor(BLACK);
						fillcircle(bx, by, br);
					}
					continue;
				}
				if (direction == 'd') {
					for (;; by += 2) {
						if (by == 590 || position[bx][by + 10])
							break;
						setcolor(RED);
						setfillcolor(RED);
						fillcircle(bx, by, br);
						Sleep(btime);
						setcolor(BLACK);
						setfillcolor(BLACK);
						fillcircle(bx, by, br);
					}
					continue;
				}
			}

			/*
			if (c == 'j') {//left
				for (int i = 1; i <= 5; x2 -= 1, i++) {
					if (x2 == 10 || position[x2 - 10][y2])
						break;
					setcolor(BLUE);
					setfillcolor(BLUE);
					fillcircle(x2, y2, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x2, y2, 10);
				}
			}
			if (c == 'l') {//right
				for (int i = 1; i <= 5; i++, x2 += 1) {
					if (x2 == 1490 || position[x2 + 10][y2])
						break;
					setcolor(BLUE);
					setfillcolor(BLUE);
					fillcircle(x2, y2, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x2, y2, 10);
				}
			}
			if (c == 'i') {//up
				for (int i = 1; i <= 5; i++, y2 -= 1) {
					if (y2 == 10 || position[x][y2 - 10])
						break;
					setcolor(BLUE);
					setfillcolor(BLUE);
					fillcircle(x2, y2, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x2, y2, 10);
				}
			}
			if (c == 'k') {//down
				for (int i = 1; i <= 5; i++, y2 += 1) {
					if (y2 == 590 || position[x2][y2 + 10])
						break;
					setcolor(BLUE);
					setfillcolor(BLUE);
					fillcircle(x2, y2, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x2, y2, 10);
				}
			}
			*/
			//退出
			if (c == 27) {
				break;
			}

		}
		
	}
	//closegraph();
	return 0;
}

void displayGrass() {                      //grassSquare: 300*200   color:green
	srand((unsigned)time(NULL));
	int a, b;
	for (int i = 1; i <= 3; i++) {
		a = getRandData(20, 1200);
		b = getRandData(20, 400);
		while (Judge(a, b)) {
			a = getRandData(20, 1200);
			b = getRandData(20, 400);
		}
		setcolor(GREEN);
		setfillcolor(GREEN);
		fillrectangle(a, b, a + 300, b + 200);
		for (int j = a; j <= a + 300; j++)
			for (int k = b; k <= b + 200; k++) {
				position[j][k] = 1;
			}
	}
}

int getRandData(int a, int b)
{
	return (rand() % (b - a + 1)) + a;
}

bool Judge(int a, int b) {
	if (position[a][b])
		return 1;
	if (position[a + 300][b])
		return 1;
	if (position[a][b + 200])
		return 1;
	if (position[a + 300][b + 200])
		return 1;
	return 0;
}

bool Judge2(int a, int b) {
	if (position[a][b])
		return 1;
	if (position[a + 100][b])
		return 1;
	if (position[a][b + 100])
		return 1;
	if (position[a + 100][b + 100])
		return 1;
	return 0;
}

void left() {
	char c = 0;
	for (;; x -= 2) {
		if (x == 10 || position[x - 10][y])
			break;
		setcolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, 10);
		Sleep(15);
		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 10);

		if (_kbhit())
		{
			c = _getch();
			break;
		}
	}
	switch (c) {
	case 0:break;
	case 'a': left(); break;
	case 'd': right(); break;
	case 'w': up(); break;
	case 's': down(); break;
	case 27: exit(0);
	}
	right();
}

void right() {
	char c = 0;
	for (;; x += 2) {
		if (x == 1490 || position[x + 10][y])
			break;
		setcolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, 10);
		Sleep(15);
		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 10);


		if (_kbhit())
		{
			c = _getch();
			break;
		}
	}
	switch (c) {
	case 0:break;
	case 'a': left(); break;
	case 'd': right(); break;
	case 'w': up(); break;
	case 's': down(); break;
	case 27: exit(0);
	}
	left();
}

void up() {
	char c = 0;
	for (;; y -= 2) {
		if (y == 10 || position[x][y - 10])
			break;
		setcolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, 10);
		Sleep(15);
		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 10);

		if (_kbhit())
		{
			c = _getch();
			break;
		}
	}
	switch (c) {
	case 0:break;
	case 'a': left(); break;
	case 'd': right(); break;
	case 'w': up(); break;
	case 's': down(); break;
	case 27: exit(0);
	}
	down();
}

void down() {
	char c = 0;
	for (;; y += 2) {
		if (y == 590 || position[x][y + 10])
			break;
		setcolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, 10);
		Sleep(15);
		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 10);

		if (_kbhit())
		{
			c = _getch();
			break;
		}
	}
	switch (c) {
	case 0:break;
	case 'a': left(); break;
	case 'd': right(); break;
	case 'w': up(); break;
	case 's': down(); break;
	case 27: exit(0);
	}
	up();
}

void displayBox() {     //盒子大小100*100，初始15个, color:brown
	srand((unsigned)time(NULL));
	int a, b;
	for (int i = 1; i <= 15; i++) {
		a = getRandData(20, 1400);
		b = getRandData(20, 500);
		while (Judge2(a, b)) {
			a = getRandData(20, 1400);
			b = getRandData(20, 500);
		}
		setcolor(BROWN);
		setfillcolor(BROWN);
		fillrectangle(a, b, a + 100, b + 100);
		for (int j = a; j <= a + 100; j++)
			for (int k = b; k <= b + 100; k++) {
				position[j][k] = 1;
			}
	}
}